package com.zzs.covid19_view.common.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zzs.covid19_view.common.entity.AreaStat;
import com.zzs.covid19_view.common.entity.Statistics;
import com.zzs.covid19_view.common.entity.TimeLine;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名: Parse
 * 创建者: zzs
 * 创建时间: 2021/8/12 11:19
 * 描述: 解析类
 */
@Slf4j
public class Parse {
    /**
     * 解析TimeLine的JSON数据
     *
     * @param timelineServiceInformation
     * @return
     */
    public static List<TimeLine> parseTimeLineInformation(String timelineServiceInformation) {
        List<TimeLine> list = new ArrayList<>();
        JSONArray jsonArray = JSON.parseArray(timelineServiceInformation);
        for (Object jsonObj : jsonArray) {
            TimeLine timeLine = JSON.toJavaObject((JSON) jsonObj, TimeLine.class);
            timeLine.setCreateTime((timeLine.getCreateTime() == null ? 0 : timeLine.getCreateTime()) / 1000);
            timeLine.setModifyTime((timeLine.getModifyTime() == null ? 0 : timeLine.getModifyTime()) / 1000);
            timeLine.setPubDate((timeLine.getPubDate() == null ? 0 : timeLine.getPubDate()) / 1000);
            list.add(timeLine);
            log.info("TimeLine:{}", timeLine);
        }
        return list;
    }

    /**
     * 解析StatisticsJSON数据
     *
     * @param staticInformation
     * @return
     */
    public static Statistics parseStatisticsInformation(String staticInformation) {
        JSONObject jsonObj = JSON.parseObject(staticInformation);
        Statistics statistics = JSON.toJavaObject(jsonObj, Statistics.class);
        statistics.setCreateTime((statistics.getCreateTime() == null ? 0 : statistics.getCreateTime()) / 1000);
        statistics.setModifyTime((statistics.getModifyTime() == null ? 0 : statistics.getModifyTime()) / 1000);
        log.info("Statistics:{}", statistics);
        return statistics;
    }

    /**
     * 解析地区信息JSON数据
     *
     * @param areaInformation
     * @return
     */
    public static List<AreaStat> parseAreaInformation(String areaInformation) {
        List<AreaStat> list = new ArrayList<>();
        JSONArray jsonArray = JSON.parseArray(areaInformation);
        for (Object jsonObj : jsonArray) {
            AreaStat areaStat = JSON.toJavaObject((JSON) jsonObj, AreaStat.class);
            list.add(areaStat);
            log.info("AreaStat:{}", areaStat);
        }
        return list;
    }
}
